package com.qf.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.qf.dao.MeetingPubMapper;
import com.qf.dao.MeetingTypeMapper;
import com.qf.entity.DTO.MeetingDTO;
import com.qf.entity.po.MeetingPub;

import com.qf.entity.po.MeetingPubExample;
import com.qf.entity.po.MeetingType;
import com.qf.service.MeetingService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @version : 1.0
 * @author:宋煜
 * @date: 2022/5/27 19:45
 */
@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingPubMapper meetingPubMapper;
    @Autowired
    private MeetingTypeMapper meetingTypeMapper;
    @Override
    public List<MeetingPub> getMeetingList() {
        return meetingPubMapper.getMeetingList();
    }

    /**
     * TODO 自动生成会议编号逻辑
     */
    @Override
    public String getPCode(String date) {
        String s1 = date.replaceAll("T", " ");
        String substring = date.substring(0, 10);
        String s = substring.replaceAll("-", "");
        long l = Long.parseLong(s);
        String s2 = String.valueOf(l+1);
        String end = s2.substring(0,4)+"-"+s2.substring(4,6)+"-"+s2.substring(6,8);
        String start = s + "001";
        MeetingPubExample example = new MeetingPubExample();
        example.createCriteria().andPtimeBetween(substring,end);
        List<MeetingPub> meetingPubs = meetingPubMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(meetingPubs)){
            return start;
        }
        List<Long> list = new ArrayList<>();
        for (MeetingPub m:meetingPubs){
            list.add(Long.parseLong(m.getPcode()));
        }
        Long max = Collections.max(list);
        return String.valueOf(max+1);
    }

    /**
     * TODO 查找会议类型
     */
    @Override
    public List<MeetingType> getTypeList() {
        return meetingTypeMapper.selectByExample(null);
    }

    /**
     * TODO 新增会议
     *
     * @param meetingPub
     */
    @Override
    public void insertMeeting(MeetingPub meetingPub) {
        meetingPub.setPtime(meetingPub.getPtime().replaceAll("T"," "));
        meetingPubMapper.insert(meetingPub);
    }

    /**
     * TODO 删除会议
     *
     * @param id
     */
    @Override
    public void delMeeting(String id) {
        meetingPubMapper.deleteByPrimaryKey(id);
    }

    /**
     * TODO 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(String[] ids) {
        for (String id : ids){
            meetingPubMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * TODO  根据id查询对象
     *
     * @param id
     */
    @Override
    public MeetingPub selectById(String id) {
        MeetingPub meetingPub = meetingPubMapper.selectByPrimaryKey(id);
        meetingPub.setTname(meetingTypeMapper.selectByPrimaryKey(meetingPub.getTid()).getTname());
        return meetingPub;
    }

    /**
     * TODO 更新
     *
     * @param meetingPub
     */
    @Override
    public void updateMeeting(MeetingPub meetingPub) {
        meetingPub.setPtime(meetingPub.getPtime().replaceAll("T", " "));
        meetingPub.setPcode(this.getPCode(meetingPub.getPtime()));
        meetingPubMapper.updateByPrimaryKey(meetingPub);
    }

    /**
     * TODO 更新未完会===完会
     *
     * @param id
     */
    @Override
    public void updateStatusDown(String id) {
        meetingPubMapper.updateStatusDown(id);
    }

    @Override
    public void updateStatusUp(String id) {
        meetingPubMapper.updateStatusUp(id);
    }

    /**
     * TODO 列表查询
     *
     * @param meetingDTO
     */
    @Override
    public List<MeetingPub> listCondition(MeetingDTO meetingDTO) {
        return meetingPubMapper.listCondition(meetingDTO);
    }

    /**
     * TODO 查询出表中所有的tname
     */
    @Override
    public List<String> getAllTName() {
        return meetingTypeMapper.getAllTName();
    }

    /**
     * TODO 表格插入数据
     */
    @Override
    public int insertType(List<MeetingType> meetingTypes) {
        int count = 0;
        for (MeetingType meetingType:meetingTypes){
            int insert = meetingTypeMapper.insert(meetingType);
            count++;
        }
        return count;
    }

    /**
     * TODO 下载指定条件的会议表
     *
     * @param meetingDTO
     */
    @Override
    public void downloadExcel(MeetingDTO meetingDTO) throws Exception{
        List<MeetingPub> meetingPubs = meetingPubMapper.listCondition(meetingDTO);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("会议发单表", "会议发单表"),
                MeetingPub.class, meetingPubs);
        FileOutputStream outputStream = new FileOutputStream("D:\\downloadExcel\\会议发单表.xls");
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
